# -*- coding: utf-8 -*-
# time: 2025/4/9 10:31
# file: ch01.py
# author: hanson
"""
https://www.langchain.com.cn/docs/introduction/
https://www.langchain.com.cn/docs/tutorials/llm_chain/
"""
from langchain_core.output_parsers import JsonOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_ollama import ChatOllama

template ="桌上有{number}个苹果，四个桃子和3本书共有几个水果?"
prompt =PromptTemplate.from_template(template)

# 创建模型实例
#llm = ChatOpenAI(temperature=0)
llm = ChatOllama(model="qwen2.5:7b", temperature=0.3)
# 3. Create json parser
parser = JsonOutputParser()
# 创建链  | 符号是 channel 管道意思。
chain = prompt | llm | parser

# 5. 调用链（需要提供number参数）
try:
    result = chain.invoke({"number": 5})  # 假设桌上有5个苹果
    print(result)
except Exception as e:
    print(f"发生错误: {e}")

